// pages/wode/withdrawal/withdrawal.js

import {request,getMemberUser,} from '../../request/wx';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:'',
    username:'',
    account:'',
    type:2,
    userId:wx.getStorageSync('openid'),
    frozenBalance:0.0,
    balance:0.0,
    submit:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMemberUser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submit:function(){
    let that = this;

    if(!that.data.submit){
      wx.showToast({
        title: '请不要重复点击',
        icon: 'none'
      })
      return;
    }

    let {
      money,
      username,
      account,
      type,
      userId
    } = this.data;

    if(money == ''){
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return
    }
    if(username == ''){
      wx.showToast({
        title: '请输入收款人姓名',
        icon: 'none'
      })
      return
    }
    if(account == ''){
      wx.showToast({
        title: '请输入收款账号',
        icon: 'none'
      })
      return
    }
    
    that.setData({
      submit:false
    })

    let data={money,username,account,type,userId};
    return new Promise((reslove, reject) => {
      wx.showLoading({
        title:'提交中...'
     })
      request('withdrawal/save','GET',data).then(res=>{
        reslove(res)
        wx.hideLoading()
        if(res.code == 200){
          wx.showToast({
            title: '提交成功',
            icon: 'none'
          })
          that.setData({
            money: '',
            username: '',
            account: '',
            type:2,
          })
          that.getMemberUser();
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
        
        that.setData({
          submit:true
        })
      })
    })
  },
  getMemberUser:function(){
    let that = this;
    let openid = wx.getStorageSync('openid');
    getMemberUser({openid}).then(res=>{
      if(res.code == 200){
        that.setData({
          balance:res.data.balance,
          frozenBalance:res.data.frozenBalance
        })
      }
    });
  }
})